package com.artron.mediaartron.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.artron.mediaartron.R;


/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-20.
 */

public class PagerIndicator extends FrameLayout {

    private int mPointRes;
    private int mCurrentPointRes;
    private int mOrientation;
    private LinearLayout mLlWhitePoint;
    private int mDimension;

    private boolean isFirstStart = true;
    private ImageView mCurrentIv;

    public PagerIndicator(Context context) {
        this(context, null);
    }

    public PagerIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PagerIndicator);
        mCurrentPointRes = ta.getResourceId(R.styleable.PagerIndicator_shape_current, R.drawable.shape_rectangle_black);
        mPointRes = ta.getResourceId(R.styleable.PagerIndicator_shape, R.drawable.shape_rectangle_white);
        mOrientation = ta.getInt(R.styleable.PagerIndicator_orientation, 1);
        mDimension = ((int) ta.getDimension(R.styleable.PagerIndicator_shape_size, 10));
    }

    @Override
    protected void onFinishInflate() {
        if (isFirstStart) {
            mLlWhitePoint = new LinearLayout(getContext());
            mLlWhitePoint.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            mLlWhitePoint.setOrientation(mOrientation == 0 ?
                    LinearLayout.VERTICAL : LinearLayout.HORIZONTAL);
            isFirstStart = false;
        }
    }

    public void setViewPager(ViewPager viewPager) {
        PagerAdapter adapter = viewPager.getAdapter();
        LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
                mDimension, mDimension);
        if (mOrientation == 0) {
            linearLayoutParams.setMargins(10, 0, 10, mDimension);
        } else {
            linearLayoutParams.setMargins(0, 10, mDimension, 10);
        }
        for (int x = 0; x < adapter.getCount(); x++) {
            ImageView iv = new ImageView(getContext());
            iv.setLayoutParams(linearLayoutParams);
            iv.setImageResource(mPointRes);
            mLlWhitePoint.addView(iv);
        }
        this.addView(mLlWhitePoint);
        FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(mDimension,
                mDimension);
        if (mOrientation == 0) {
            frameLayoutParams.setMargins(10, 0, 10, mDimension);
        } else {
            frameLayoutParams.setMargins(0, 10, mDimension, 10);
        }
        mCurrentIv = new ImageView(getContext());
        mCurrentIv.setLayoutParams(frameLayoutParams);
        mCurrentIv.setImageResource(mCurrentPointRes);
        this.addView(mCurrentIv);

        viewPager.addOnPageChangeListener(new OnPagerChangeListener());
    }

    private class OnPagerChangeListener extends ViewPager.SimpleOnPageChangeListener {

        private int prePosition = 0;
        private float transitionValue;
        private float distance;

        private OnPagerChangeListener() {
            distance = 2 * mDimension;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if (position == prePosition) {
                float offsetDistance = transitionValue + distance * positionOffset;
                if (offsetDistance > transitionValue + distance) {
                    offsetDistance = transitionValue + distance;
                }
                if (mOrientation == 0) {
                    ViewCompat.setTranslationY(mCurrentIv, offsetDistance);
                } else {
                    ViewCompat.setTranslationX(mCurrentIv, offsetDistance);
                }
            } else {
                transitionValue = getTransitionValue(position);
            }

            prePosition = position;
        }

        private float getTransitionValue(int position){
            float value;
            if (position > prePosition) {
                value = position * distance;
            } else {
                value = (prePosition - 1) * distance;
                if (value < 0) {
                    value = 0;
                }
            }
            return value;
        }
    }
}
